import {
    getLeaveRecord,
    getMyattenddetail,
    getLeaveTypeList,
    attendLeaveExport
} from '@/services/attendance/abnormalRank';
import { dictionaryList } from '@/services/dictionary';

export default {
    namespace: 'leaveRecord',
    state: {
        leaveData: [],
        pagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        leaveTypeList: [],
        leaveStatus: []
    },
    effects: {
        *fetchList({ payload }, { put, call }) {
            const result = yield call(getLeaveRecord, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        leaveData: result.data.rows,
                        pagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 打卡记录
        *fetchMydetail({ payload }, { put, call }) {
            const result = yield call(getMyattenddetail, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        attenddetail: result.data.rows
                    }
                });
            }
        },
        // 请假记录请假类型
        *fetchLeaveTypeList({ payload, callback }, { put, call }) {
            const result = yield call(getLeaveTypeList, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        leaveTypeList: result.data
                    }
                });
                if (callback) callback();
            }
        },
        // 请假记录导出
        *fetchExport({ payload }, { call }) {
            const res = yield call(attendLeaveExport, payload);
            return res;
        },
        /**
         * @param {[string]} codes 字典code集合
         * isShowChildFormat 是否展示树状 1展示 0 不展示,只有 codes 传省市区才有多级，默认不做树状展示
         * 想要省市区树形数据 调用 getCascaderOptions 方法获取
         */
        *fetchDictionaryList({ payload, callback }, { put, call }) {
            const response = yield call(dictionaryList, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        leaveStatus: response.data
                    }
                });
            }
            if (callback) callback();
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        }
    }
};
